﻿<%@ WebHandler Language="C#" Class="GetInfo" %>

using System;
using System.Web;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

public class GetInfo : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";

        string type = context.Request.QueryString["type"];

        switch (type)
        {
            case "GetUser":
                GetUser(context);
                break;
            case "GetUserCount":
                GetUserCount(context);
                break;
            case "GetRole":
                GetRole(context);
                break;
            case "GetRoleCount":
                GetRoleCount(context);
                break;
        }



        //GetWorker.GetInfoWebService g = new GetWorker.GetInfoWebService();
        //context.Response.Write(g.getWorkerInfo("hIBPyzelr8zMZtni3HIkp6JxNXlGSRPGD1AxMzo3c+2UhmR8FXHVVtG/RytrryHj6DEun1/Z+TmrwDIe0p155/ljNUCSdTfy9TCKngsK79Q="));
        //context.Response.Write("Hello World");
    }
    /// <summary>
    /// 获取用户总数
    /// </summary>
    /// <param name="context"></param>
    private static void GetUserCount(HttpContext context)
    {
        DataTable dt = ExecuteTable("server=sql.lghy.com;uid=sa;pwd=ggyy3863781;database=hdjcw_hdj_20120913122809", CommandType.Text, "select count(*) from Base_Worker", null);

        context.Response.Write(dt.Rows[0][0].ToString());
    }
    /// <summary>
    /// 分页查询用户
    /// </summary>
    /// <param name="context"></param>
    public static void GetUser(HttpContext context)
    {
        context.Response.ContentType = "text/plain";

        string rescurrPage = context.Request.QueryString["currPage"];
        string respageSize = context.Request.QueryString["pageSize"];
        string resSQL = "select top " + respageSize + " Worker_Number,Worker_Name from Base_Worker where id not in (select top " + ((int.Parse(rescurrPage) - 1) * int.Parse(respageSize)) + " id from Base_Worker order by Worker_Name)order by Worker_Name";
        Dictionary<string, object> result = new Dictionary<string, object>();
        string resultInfo = "";
        DataTable dt = ExecuteTable("server=sql.lghy.com;uid=sa;pwd=ggyy3863781;database=hdjcw_hdj_20120913122809", CommandType.Text, resSQL, null);
        foreach (DataRow dr in dt.Rows)
        {
            if (resultInfo.Length > 0)
                resultInfo += ",";
            resultInfo += "\"" + dr["Worker_Number"].ToString() + "\":{\"Account\":\"\",\"RealName\":\"" + dr["Worker_Name"].ToString() + "\",\"id\":\"" + dr["Worker_Number"].ToString() + "\",\"text\":\"" + dr["Worker_Name"].ToString() + "\",\"value\":\"\",\"parentId\":\"\",\"showcheck\":true,\"img\":\"\"}";
        }

        //context.Response.Write("{" + Content + "}");
        context.Response.Write("{" + resultInfo + "}");
    }

    /// <summary>
    /// 获取角色总数
    /// </summary>
    /// <param name="context"></param>
    private static void GetRoleCount(HttpContext context)
    {
        DataTable dt = ExecuteTable("server=sql.lghy.com;uid=sa;pwd=ggyy3863781;database=hdjcw_hdj_20120913122809", CommandType.Text, "select count(*) from Base_Group", null);

        context.Response.Write(dt.Rows[0][0].ToString());
    }
    /// <summary>
    /// 分页查询角色
    /// </summary>
    /// <param name="context"></param>
    public static void GetRole(HttpContext context)
    {
        context.Response.ContentType = "text/plain";

        string rescurrPage = context.Request.QueryString["currPage"];
        string respageSize = context.Request.QueryString["pageSize"];
        string resSQL = "select top " + respageSize + " Group_Number,Group_Name from Base_Group where id not in (select top " + ((int.Parse(rescurrPage) - 1) * int.Parse(respageSize)) + " id from Base_Group order by Group_Name)order by Group_Name";
        Dictionary<string, object> result = new Dictionary<string, object>();
        string resultInfo = "";
        DataTable dt = ExecuteTable("server=sql.lghy.com;uid=sa;pwd=ggyy3863781;database=hdjcw_hdj_20120913122809", CommandType.Text, resSQL, null);
        foreach (DataRow dr in dt.Rows)
        {
            if (resultInfo.Length > 0)
                resultInfo += ",";
            resultInfo += "\"" + dr["Group_Number"].ToString() + "\":{\"Account\":\"\",\"RealName\":\"" + dr["Group_Name"].ToString() + "\",\"id\":\"" + dr["Group_Number"].ToString() + "\",\"text\":\"" + dr["Group_Name"].ToString() + "\",\"value\":\"\",\"parentId\":\"\",\"showcheck\":true,\"img\":\"\"}";
        }

        //context.Response.Write("{" + Content + "}");
        context.Response.Write("{" + resultInfo + "}");
    }
    /// <summary>
    /// 操作存储过程或SQL返回结果集DataTable
    /// </summary>
    /// <param name="ConObj">SqlConnection</param>
    /// <param name="CmdType">执行类型</param>
    /// <param name="StrCmdText">存储过程名或SQL命令</param>
    /// <param name="ParmObj">参数，无参数为null</param>
    /// <returns>返回结果集DataTable</returns>
    public static DataTable ExecuteTable(string StrCon, CommandType CmdType, string StrCmdText, params SqlParameter[] ParmObj)
    {
        SqlCommand CmdObj = new SqlCommand();
        using (SqlConnection ConObj = new SqlConnection(StrCon))
        {
            PrepareCommand(CmdObj, ConObj, CmdType, StrCmdText, ParmObj);
            SqlDataAdapter DadObj = new SqlDataAdapter();
            DadObj.SelectCommand = CmdObj;
            DataSet DstObj = new DataSet();
            DadObj.Fill(DstObj, "Result");
            CmdObj.Parameters.Clear();
            if (DstObj.Tables.Count > 0)
            {
                return DstObj.Tables["Result"];
            }
            return null;
        }
    }

    /// <summary>
    /// PrepareCommand
    /// </summary>
    /// <param name="CmdObj">SqlCommand</param>
    /// <param name="ConObj">SqlConnection</param>
    /// <param name="CmdType">执行类型</param>
    /// <param name="StrCmdText">存储过程名或SQL命令</param>
    /// <param name="ParmObj">参数，无参数为null</param>
    private static void PrepareCommand(SqlCommand CmdObj, SqlConnection ConObj, CommandType CmdType, string StrCmdText, SqlParameter[] ParmObj)
    {
        if (ConObj.State != ConnectionState.Open)
        {
            ConObj.Open();
        }
        CmdObj.Connection = ConObj;
        CmdObj.CommandText = StrCmdText;
        CmdObj.CommandTimeout = 1000;
        CmdObj.CommandType = CmdType;
        if (ParmObj != null)
        {
            foreach (SqlParameter parameter in ParmObj)
            {
                CmdObj.Parameters.Add(parameter);
            }
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}